import numpy as np
def solve(nums):
    n=len(nums)
    dp_max=[0]*n 
    dp_min=[0]*n 
    dp_max[0]=nums[0]
    dp_min[0]=nums[0]

    lst_max=[None]*n
    lst_min=[None]*n 
    lst_max[0]=[nums[0]]
    lst_min=nums[0]

    for i in range(1,n):
        dp_max[i]=max(nums[i],dp_max[i-1]*nums[i],dp_min[i-1]*nums[i])
        dp_min[i]=min(nums[i],dp_max[i-1]*nums[i],dp_min[i-1]*nums[i])
        
        if dp_max[i]==nums[i]: lst_max[i]=[nums[i]]
        elif dp_max[i]==dp_max[i-1]*nums[i]: lst_max[i]=lst_max[i-1]+[nums[i]]
        else: lst_max[i]=lst_min+[nums[i]]

        if dp_min[i]==nums[i]: lst_min=[nums[i]]
        elif dp_min[i]==dp_max[i-1]*nums[i]: lst_min=lst_max[i-1]+[nums[i]]
        else: lst_min=lst_min+[nums[i]]

    #print(dp_max,dp_min)
    idx=np.array(dp_max).argmax()
    print(lst_max[idx])

    return dp_max[idx]

nums = [2,3,-2,4]
print(solve(nums))
nums = [-2,0,-1]
print(solve(nums))
# def dfs(nums,start,ans,path):
#     if len(ans)==0 or len(path)>len(ans[-1]):
#         ans.append(path[:])
#     for i in range(start,len(nums)):
#         if len(path)==0 or nums[i]>path[-1]:
#             path.append(nums[i])
#             dfs(nums,i+1,ans,path)
#             path.pop()

# def solve(nums):
#     ans=[]
#     path=[]
#     dfs(nums,0,ans,path)
#     return ans[-1]

# nums = [10,9,2,5,3,7,101,18]
# print(solve(nums))
